
<include file="mobile/Public/header" />

<link rel="stylesheet" href="__CSS__/detail_font.css">
<link rel="stylesheet" href="__CSS__/makeorder.css">
<link rel="stylesheet" href="__CSS__/good.css">
<header class="color-main vertical-box">
    <span class="header-title">提交订单</span>
    <div class="header-left vertical-box">
        <img class="img-btn" src="__IMG__/back.png" onclick="history.back()">
    </div>
    <div class="header-right vertical-box">
        <img class="img-btn" src="__IMG__/home.png" onclick="window.location.href='{:U('shop/index/index')}'">
    </div>
</header>
<article class="makeorder-section btn-footer-margin">
    <php>
    if(!empty($address[0])){
        </php>
        <section class="address-section linear-section margin-top-section margin-bottom last-liner-section"
                 onclick="window.location.href='{:U('shop/index/address')}&type=select'"
                 data-id="<php> echo $address[0]['id'] </php>">
            <div class="linear-address-section border-box">
                <p class="big-text">
                    <span class="detail-icon-user active-font"></span><span class="address-user"><php> echo $address[0]['name'] </php></span>
                    <span class="detail-icon-phone2 active-font"></span><php> echo $address[0]['phone'] </php>
                </p>
                <p class="small-text tips-font">
                    <php>
                    $full_address='';
                    if(!empty($address[0]['province'])) $full_address.=$address[0]['province'];
                    if(!empty($address[0]['town'])) $full_address.=$address[0]['town'];
                    if(!empty($address[0]['city'])) $full_address.=$address[0]['city'];
                    if(!empty($address[0]['address'])) $full_address.=$address[0]['address'];
                    echo $full_address;
                    </php>
                </p>
            </div>
        <span class="linear-right vertical-box"><span>
            <img src="__IMG__/go.png">
        </span></span>
        </section>
    <php>
    }
    else{
        </php>
        <section class="address-section linear-section margin-top-section margin-bottom last-liner-section" onclick="window.location.href='{:U('shop/index/address')}&type=select'">
            <div class="linear-address-section border-box">
                <p class="big-text">请选择您的收货信息</p>
            </div>
        <span class="linear-right vertical-box"><span>
            <img src="__IMG__/go.png">
        </span></span>
        </section>
    <php>
    }
    </php>


    <php>
    /*立即购买模式*/
    if(!empty($product)){
        </php>
        <section class="good-section linear-section">
            <div class="good-section-left">
                <img class="good-section-img" src="<php> if(!empty($product['main_img'])) echo pic($product['main_img']); </php>">
            </div>
            <div class="good-section-right border-box">
                <p class="good-section-title"><php> if(!empty($product['title'])) echo $product['title']; </php></p>
                <php>
                if(!empty($sku)){
                    </php>
                    <p class="good-section-option small-text tips-font"><php> echo $sku</php></p>
                <php>
                }
                </php>
                <p class="good-section-option small-text tips-font clearfix">
                    ￥<php> echo sprintf("%.2f", $product['price']/100) </php> x <php> if(!empty($quantity)) echo $quantity</php>
                    <span class="big-text good-section-price secondary-font">￥<php> echo sprintf("%.2f", ($product['price']*$quantity)/100) </php></span>
                </p>
            </div>
        </section>
    <php>
    }
    /*购物车模式*/
    elseif(!empty($cart_list_products)){
        $cart_price = 0;//记录购物车总价格
        foreach ($cart_list_products as $c) {
            $cart_product = $c['product'];
            $cart_price+=$cart_product['price']*$c['quantity']
            </php>
            <section class="good-section linear-section">
                <div class="good-section-left">
                    <img class="good-section-img" src="<php> if(!empty($cart_product['main_img'])) echo pic($cart_product['main_img']); </php>">
                </div>
                <div class="good-section-right border-box">
                    <p class="good-section-title"><php> if(!empty($cart_product['title'])) echo $cart_product['title']; </php></p>
                    <php>
                    if(!empty($cart_product['sku_id'][1])){
                        </php>
                        <p class="good-section-option small-text tips-font"><php> echo $cart_product['sku_id'][1]</php></p>
                    <php>
                    }
                    </php>
                    <p class="good-section-option small-text tips-font clearfix">
                        ￥<php> echo sprintf("%.2f", $cart_product['price']/100) </php> x <php> if(!empty($c['quantity'])) echo $c['quantity']</php>
                        <span class="big-text good-section-price secondary-font">￥<php> echo sprintf("%.2f", ($cart_product['price']*$c['quantity'])/100) </php></span>
                    </p>
                </div>
            </section>
        <php>
        }
    }
    </php>

    <section class="delivery-section linear-section last-liner-section">
        <span class="linear-title vertical-box"><span>配送方式</span></span>
        <p class="linear-input border-box small-text tips-font">快递费：<span class="delivery-fee"></span></p>
    </section>
    <php>
    $real_price = 0;
    if(!empty($product)){
        $real_price =  sprintf("%.2f", ($product['price']*$quantity)/100);
    }
    elseif(!empty($cart_list_products)){
        $real_price =  sprintf("%.2f", $cart_price/100);
    }
    </php>
    <section class="linear-section margin-top-section"
             onclick="window.location.href='/index.php?s=shop/index/user_coupon&type=select&paid_fee=<php> echo $real_price*100 </php>'"
             data-id="<php> if(!empty($coupon['id'])) echo $coupon['id'] </php>">
        <span class="linear-title vertical-box"><span>优惠券</span></span>
        <input class="linear-input border-box tips-font" type="text" readonly placeholder="请选择使用优惠券"
               value="<php> if(!empty($coupon['info']['title'])) echo $coupon['info']['title'].'：优惠￥'.sprintf("%.2f", $coupon['info']['rule']['discount']/100) </php>">
        <span class="linear-right vertical-box"><span>
            <img src="__IMG__/go.png">
        </span></span>
    </section>



    <php>
    if(!empty($vipcard_discount)&&$vipcard_discount!=1){
        </php>
        <!--会员卡优惠-->
        <section class="linear-section">
            <span class="linear-title vertical-box"><span>会员优惠</span></span>
            <input class="linear-input border-box tips-font" type="text" value="<php> echo ($vipcard_discount*10).'折优惠：减免￥'.$real_price*$vipcard_discount </php>" readonly placeholder="">
        </section>
    <php>
    }
    </php>


    <section class="linear-section last-liner-section">
        <span class="linear-title vertical-box"><span>备注</span></span>
        <input class="remark-input linear-input border-box tips-font" type="text" value="" placeholder="请填写您的留言">
    </section>
    <!---->
<!--    <section class="linear-section margin-top-section linear-noinput">-->
<!--        <span class="small-text tips-font linear-title vertical-box"><span>请选择支付方式</span></span>-->
<!--    </section>-->
<!--    <section class="pay-type-section linear-section linear-noinput vertical-box last-liner-section margin-bottom">-->
<!--        <span class="linear-title vertical-box"><span>微信支付</span></span>-->
<!--        <span class="linear-right vertical-box"><span class="circle-checkbox active-bg">-->
<!--            <img style="margin: 0" src="__IMG__/check.png">-->
<!--        </span></span>-->
<!--    </section>-->
<!--    <section class="pay-type-section linear-section linear-noinput vertical-box last-liner-section margin-bottom">-->
<!--        <span class="linear-title vertical-box"><span>支付宝支付</span></span>-->
<!--        <span class="linear-right vertical-box"><span class="circle-checkbox">-->
<!--            <img style="margin: 0" src="__IMG__/check.png">-->
<!--        </span></span>-->
<!--    </section>-->
</article>
<footer class="btn-footer">

    <div class="makeorder-footer-left color-main big-text border-box">
        实付款：￥
        <span id="real-pay"
              data-price="<php> echo $real_price </php>"
              data-coupon="<php> if(!empty($coupon['info']['rule']['discount'])) echo $coupon['info']['rule']['discount']/100 </php>"
              data-discount="<php> if(!empty($vipcard_discount)&&$vipcard_discount!=1) echo $vipcard_discount </php>">
            <php> echo $real_price </php>
        </span>
    </div>
    <div class="makeorderBtn makeorder-footer-right color-primary big-text">提交订单</div>
</footer>

<include file="mobile/Public/footer" />


<script>
    var product_data = <php> echo(!empty($product)? json_encode($product):"null")</php>;//useful
    var address_data = <php> echo(!empty($address[0])? json_encode($address[0]):"null")</php>;//useful
    var quantity_data = <php> echo(!empty($quantity)? json_encode($quantity):"null")</php>;//useful
    var sku_data = <php> echo(!empty($sku)? json_encode($sku):"null")</php>;//useful
    var cart_data = <php> echo(!empty($cart_list_products)? json_encode($cart_list_products):"null")</php>;//useful
    var cart_id = <php> echo(!empty($cart_id)? json_encode($cart_id):"null")</php>;//useful
    var coupon_data = <php> echo(!empty($coupon)? json_encode($coupon):"null")</php>;
    //'jquery' or 'zepto' 脚本入口,按情况选择加载
    seajs.use('zepto', function () {
        $(document).ready(function () {
            seajs.use('zepto_cookie', function () {
                /*
                    下单,下单完清楚优惠券信息
                */
                $(".makeorderBtn").click(function () {
                    var address_id = $('.address-section').data('id');//
                    if(!address_id){
                        alert('请选择地址');
                        return
                    }
                    var delivery = $('.delivery-section').data('type');
                    var pay_type = 11;//微信支付
                    var data = {
                        address_id:address_id,
                        delivery:delivery,//不传就包邮
                        pay_type:pay_type,
                        info:{
                            remark:$('.remark-input').val()
                        }
                    };
                    /*立即购买*/
                    if(product_data){
                        var products = {
                            "0":{
                                sku_id:product_data['id'],
                                quantity:quantity_data
                            }
                        };
                        if(sku_data){
                            products['0']['sku_id'] += ';'+sku_data
                        }
                        data['products'] = products
                    }
                    /*购物车*/
                    else if(cart_data){
                        data['cart_id'] = cart_id
                    }
                    /*优惠券*/
                    if(coupon_data){
                        data['coupon_id'] = coupon_data['id'];
                    }
                    $.post('{:U('shop/index/makeorder')}',data,function (ret) {
                        ret = JSON.parse(ret);
                        if(ret.status==1){
                            $.fn.cookie('coupon_id',null);//清除
                            showTip('','下单成功','1000');
                            window.location.replace('/index.php/shop/index/jsApiPay/order_id/'+ret.info);
                        }
                        else
                        {
                            showTip('',ret.info,'1000');
//                            history.back();
                            setTimeout(function () {

                            },1000);
                        }

                    })
                });
                /*
                    根据cookie刷新页面获取新的内容，解决微信的缓存
                */
                var freshen_data = $.fn.cookie('freshen_data');
                if(freshen_data=='true'){
                    $.fn.cookie('freshen_data','false');
                    window.location.reload()
                }
            });

            /*付款数整合计算*/
            function countPrice(ele){
                var base_price = parseFloat(ele.data('price'));
                if(!base_price){return NaN}
                /*折扣先算*/
                var discount_price = parseFloat(ele.data('discount'));
                if(discount_price){
                    base_price*=discount_price
                }
                /*加减类优惠券*/
                var coupon_price = parseFloat(ele.data('coupon'));
                if(coupon_price){
                    base_price-=coupon_price
                }
                /*最后算邮费*/
                /*邮费*/
                var delivery_price = parseFloat(ele.data('delivery'));
                if(delivery_price){
                    base_price+=delivery_price
                }
                return Math.max(0,base_price);
            }
            $('#real-pay').text(countPrice($('#real-pay')).toFixed(2));//自调用初始化一次
            /*
                获取邮费信息,也需要刷新页面更新地址产品信息
            */
            if(address_data){
                /*地址信息*/
                var data = {
                    province:address_data.province,
                    city:address_data.city,
                    town:address_data.town
                };
                /*单物品信息*/
                if(product_data){
                    data['id'] = product_data.id;
                    data['quantity'] = quantity_data
                }
                /*购物车多物品信息*/
                else if(cart_data){
                    var products = {};
                    $.each(cart_data, function (index) {
                        products[index] = {
                            id:this.product.id,
                            quantity:this.quantity
                        }
                    });
                    console.log(products);
                    data['products'] = products
                }
                $.post('{:U('shop/index/preview_delivery')}', data, function (ret) {
                    ret = JSON.parse(ret);
//                    console.log(ret.status);
                    if(ret.status==1){
                        /*配送方式显示数据*/
                        var delivery_fee = (ret.info.express)? (ret.info.express/100).toFixed(2)+'元':'包邮';
                        $('.delivery-fee').text(delivery_fee);
                        /*为提交的数据记录一下*/
                        $('.delivery-section').data('type',(ret.info.express)?'express':'');
                        /*old:实付款更新
                        var old_price = parseInt($('#real-pay').text());
                        var delivery_price = ret.data.mail ? (ret.data.mail/100):0;
                        var new_price = old_price + delivery_price;
                        $('#real-pay').text(new_price.toFixed(2))*/
                        /*整合计算*/
                        $('#real-pay')
                            .data('delivery',ret.info.express ? (ret.info.express/100):0)
                            .text(countPrice($('#real-pay')).toFixed(2));
                    }
                })
            }
        })
    });
</script>
</body>
</html>